//
//  CYTopTitleBGColorViewController.m
//  CYTabBarViewController
//
//  Created by lc4y on 2017/12/13.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "CYTopTitleBGColorViewController.h"
#import "CYTopTitleCollectionViewCell.h"

@interface CYTopTitleBGColorViewController ()

@end

@implementation CYTopTitleBGColorViewController {
    CYTopTitleCollectionViewCell *_preTopTitleCollectionViewCell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_cy_collectionView registerNib:[UINib nibWithNibName:@"CYTopTitleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CYCollectionCell];
    _cy_collectionView.backgroundColor = [UIColor clearColor];
    
    [self configSubViews];
}

- (void)configSubViews {
    if(![self.cy_tabBarController.titleView superview]){
        [self.view addSubview:self.cy_tabBarController.titleView];
        [self.view addSubview:_cy_collectionView];
    }
    
    [self cy_configLayoutSubViews];
}

- (void)cy_configLayoutSubViews {
    [super cy_configLayoutSubViews];
    [self.cy_tabBarController.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)cy_calcItemSpace {
    _itemSpace = 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, _itemSpace, 0, _itemSpace);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return _itemSpace;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return _itemSpace;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *veiwController = _cy_dataArray[indexPath.row];
    CGFloat width = [self stringWidth:veiwController.cy_tabBarItem.cy_titleN];
    if(veiwController.cy_tabBarItem.cy_normalImageN){
        UIImage *image = [UIImage imageNamed:veiwController.cy_tabBarItem.cy_normalImageN];
        width += image.size.width + 5;
    }
    
    return CGSizeMake(width + 40, 40);
}

- (void)cy_refreshCollectionCell:(UICollectionViewCell *)collectionCell indexPath:(NSIndexPath *)indexPath {
    CYTopTitleCollectionViewCell *topTitleCell = (CYTopTitleCollectionViewCell *)collectionCell;
    [topTitleCell refreshCollectionCell:_cy_dataArray[indexPath.row] titleView:self.cy_tabBarController.titleView indexPath:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cy_currentIndex = indexPath.item;
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(topViewController)]) {
        [self.delegate topTitleViewMenuClick:indexPath.item];
    }
}

- (void)setCy_currentIndex:(NSInteger)cy_currentIndex {
    if( cy_currentIndex >= _cy_dataArray.count ) {
        return;
    }
    
    [_cy_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cy_currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    CGFloat delaySec = 0.000001;
    // 延时,解决快速滑动底部的视图出现不能选中的问题
    if (labs(_cy_currentIndex - cy_currentIndex) > 2) {
        delaySec = 0.2;
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySec * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _preTopTitleCollectionViewCell.titleButton.selected = NO;
        _preTopTitleCollectionViewCell.buttonBGView.backgroundColor = self.cy_tabBarController.titleView.cy_normalBGColor;
        _preTopTitleCollectionViewCell = (CYTopTitleCollectionViewCell *)[_cy_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:cy_currentIndex inSection:0]];
        _preTopTitleCollectionViewCell.titleButton.selected = YES;
        _preTopTitleCollectionViewCell.buttonBGView.backgroundColor = self.cy_tabBarController.titleView.cy_selectBGColor;
    });
    
    _cy_currentIndex = cy_currentIndex;
}

@end
